package cn.weixin.song.model;

import java.util.Date;
import java.util.Map;
import java.util.Set;

import cn.weixin.song.model.base.BaseUser;
import cn.weixin.song.util.WeixinUtils;

import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.github.sd4324530.fastweixin.message.BaseMsg;
import com.github.sd4324530.fastweixin.message.TextMsg;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.imiyue.model.user.User;
import com.jcbase.core.model.Condition;
import com.jcbase.core.model.Operators;
import com.jcbase.core.util.CommonUtils;
import com.jcbase.core.util.RandomUtils;
import com.jcbase.core.view.InvokeResult;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class User extends BaseUser<User> {
	public static final User dao = new User();
	public static Object LOCK=new Object();
	public User initUser(String wid, String fromUserName) {
		App app=App.dao.getApp(wid);
		User user=getUser(app.getId(), fromUserName);
		if(user==null){
			try{
				GetUserInfoResponse userInfoResponse=WeixinUtils.getUserAPI(wid).getUserInfo(fromUserName);
				if(userInfoResponse!=null){
					user=new User();
					user.setOpenid(fromUserName);
					user.setCreateTime(new Date());
					user.setNickname(userInfoResponse.getNickname());
					if(userInfoResponse.getSex()!=null){
						user.setSex(userInfoResponse.getSex());
					}
					user.setSubscribe(true);
					user.setAppId(app.getId());
					user.setCity(userInfoResponse.getCity());
					user.setProvince(userInfoResponse.getProvince());
					user.setLanguage(userInfoResponse.getLanguage());
					user.setHeadimgurl(userInfoResponse.getHeadimgurl());
					user.save();
				}
			}catch(Exception e){
				e.printStackTrace();
			}
	
		}
		return user;
	}
	public User getUser(int appId, String openid){
		Set<Condition> conditions=Sets.newHashSet();
		conditions.add(new Condition("openid",Operators.EQ,openid));
		conditions.add(new Condition("app_id",Operators.EQ,appId));
		return this.get(conditions);
	}
	public InvokeResult updateUserInfo(int appId, String openid,
			String mobilephone) {
		User user=this.getUser(appId,openid);
		if(user!=null){
			user.setMobilephone(mobilephone);
			user.setModifiedTime(new Date());
			user.update();
			return InvokeResult.success();
		}
		return InvokeResult.failure("用户不存在");
	}
	public String getMyInviteCode(Integer id) {
		// TODO Auto-generated method stub
		User user=User.dao.findByIdLoadColumns(id, "invite_code,id");
		if(StrKit.isBlank(user.getStr("invite_code"))){
			synchronized (LOCK) {
				String invite_code=RandomUtils.getRandomDigit(7);
				if(User.dao.getCount(CommonUtils.getConditions(new Condition("invite_code",Operators.EQ,invite_code)))==0l){
					user.set("invite_code", invite_code).update();
				}
			}
		}
		return user.getStr("invite_code");
	}
	
	public BaseMsg beInvite(Integer uid, String invite_code) {
		User user=this.get(CommonUtils.getConditions(new Condition("invite_code",Operators.EQ,invite_code)), "id");
		if(user==null){
			return new TextMsg("邀请码"+invite_code+"不存在");
		}
		if(user.getInt("id").equals(uid)){
			return new TextMsg("不能邀请自己哦");
		}
		Db.update("update user set gold_coin=gold_coin+10 where id=? or id=?", uid,user.getInt("id"));
		return new TextMsg("被成功邀请奖励您10个金币");
	}
	
	public void updateBaseInfo(Integer uid, String realname, String mobilephone) {
		// TODO Auto-generated method stub
		Map<String,Object> newValues=Maps.newHashMap();
		newValues.put("realname", realname);
		newValues.put("mobilephone", mobilephone);
		this.update(CommonUtils.getConditions(new Condition("id",Operators.EQ,uid)), newValues);
	}
	public boolean mobileHasExist(int uid,String mobilephone){
		User user=this.get(CommonUtils.getConditions(new Condition("mobilephone",Operators.EQ,mobilephone)),"id");
		if(user!=null&&!user.getInt("id").equals(uid)){
			return true;
		}
		return false;
	}
}
